package com.imedway.ihe.dtz.xds.dao.camel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLClassification;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.AssigningAuthority;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.Association;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.AssociationLabel;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.AssociationType;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.Author;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.AvailabilityStatus;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.Code;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.DocumentEntry;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.Folder;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.Identifiable;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.LocalizedString;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.ObjectReference;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.PatientInfo;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.Person;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.ReferenceId;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.SubmissionSet;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.Version;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.FindDocumentsByReferenceIdQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.FindDocumentsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.FindFoldersQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.FindSubmissionSetsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetAllQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetAssociationsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetDocumentsAndAssociationsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetDocumentsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetFolderAndContentsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetFoldersForDocumentQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetFoldersQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetRelatedDocumentsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetSubmissionSetAndContentsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.GetSubmissionSetsQuery;
import org.openehealth.ipf.commons.ihe.xds.core.responses.QueryResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.imedway.ihe.dd.vo.FindDocumentsVO;
import com.imedway.ihe.dd.vo.FindFoldersVO;
import com.imedway.ihe.dd.vo.FindSubmissionSetsVO;
import com.imedway.ihe.dd.vo.GetAllVO;
import com.imedway.ihe.dd.vo.GetAssociationsVO;
import com.imedway.ihe.dd.vo.GetDocumentsAndAssociationsVO;
import com.imedway.ihe.dd.vo.GetFolderAndContentsVO;
import com.imedway.ihe.dd.vo.GetFoldersForDocumentVO;
import com.imedway.ihe.dd.vo.GetSubmissionSetsVO;
import com.imedway.ihe.dtz.xds.cache.DataCacheMap;
import com.imedway.ihe.dtz.xds.entity.camel.XdsAssociationPO;
import com.imedway.ihe.dtz.xds.entity.camel.XdsDocumentEntryPO;
import com.imedway.ihe.dtz.xds.entity.camel.XdsFolderPO;
import com.imedway.ihe.dtz.xds.entity.camel.XdsSubmissionSetPO;
import com.imedway.ihe.dtz.xds.util.BuildVoUtil;
import com.imedway.ihe.dtz.xds.util.DateUtils;
import com.imedway.ihe.dtz.xds.util.JaxbMapperUtils;
import com.imedway.ihe.dtz.xds.vo.GetDocumentsVO;
import com.imedway.ihe.dtz.xds.vo.GetSubmissionSetAndContentsVO;

import lombok.extern.slf4j.Slf4j;

/**
 * @author dtz <br/>
 * @date 2024/7/4
 */
@Slf4j
@Repository
public class Iti18Dao_CAMEL{
	
	@Autowired
	private FindBaseDao_CAMEL findBaseDao_CAMEL;
	
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	public QueryResponse FIND_DOCUMENTS(FindDocumentsQuery findDocumentsQuery) {
		
		QueryResponse queryResponse = new QueryResponse();		
		FindDocumentsVO vo = BuildVoUtil.buildFindDocumentsVO(findDocumentsQuery);	
		List<String>  idList=findBaseDao_CAMEL.findExtrinsicobject(vo);
		List<ObjectReference> objectReferenceList = new ArrayList<ObjectReference>();
		for(String s:idList) {
			ObjectReference o = new ObjectReference();
			o.setId(s);
			objectReferenceList.add(o);
		}	
		queryResponse.setReferences(objectReferenceList);;
		log.info(JaxbMapperUtils.convertToXml(queryResponse, "UTF-8", true));

		return queryResponse;
		
	}
	
	public QueryResponse FIND_SUBMISSION_SETS(FindSubmissionSetsQuery findDocumentsQuery) {
		FindSubmissionSetsVO vo = BuildVoUtil.buildFindSubmissionSetsVO(findDocumentsQuery);
		QueryResponse queryResponse = new QueryResponse();	
		List<String>  idList=findBaseDao_CAMEL.FIND_SUBMISSION_SETS(vo);
		List<ObjectReference> objectReferenceList = new ArrayList<ObjectReference>();
		for(String s:idList) {
			ObjectReference o = new ObjectReference();
			o.setId(s);
			objectReferenceList.add(o);
		}	
		queryResponse.setReferences(objectReferenceList);;
		log.info(JaxbMapperUtils.convertToXml(queryResponse, "UTF-8", true));

		return queryResponse;
		
	}
	public QueryResponse  FIND_FOLDERS(FindFoldersQuery findDocumentsQuery) {
		FindFoldersVO vo = BuildVoUtil.buildFindFoldersVO(findDocumentsQuery);
		QueryResponse queryResponse = new QueryResponse();	
		List<String>  idList=findBaseDao_CAMEL.FIND_FOLDERS(vo);
		List<ObjectReference> objectReferenceList = new ArrayList<ObjectReference>();
		for(String s:idList) {
			ObjectReference o = new ObjectReference();
			o.setId(s);
			objectReferenceList.add(o);
		}	
		queryResponse.setReferences(objectReferenceList);;
		log.info(JaxbMapperUtils.convertToXml(queryResponse, "UTF-8", true));

		return queryResponse;
		
	}
	public QueryResponse  GET_DOCUMENTS(GetDocumentsQuery getDocumentsQuery) {
		QueryResponse queryResponse = new QueryResponse();
		GetDocumentsVO vo = BuildVoUtil.buildGetDocumentsVO(getDocumentsQuery);
		//查文档
		List<XdsDocumentEntryPO> xdsDocumentEntryPOList= findBaseDao_CAMEL.GET_XdsDocumentEntryPOs(vo);
		List<DocumentEntry> documentEntryList = new ArrayList<DocumentEntry>();
		for(XdsDocumentEntryPO po:xdsDocumentEntryPOList) {
	        // 组装DocumentEntry
	        DocumentEntry docEntry = new DocumentEntry();
			xdsDocumentEntryPOToDocumentEntry(po,docEntry);			
	        documentEntryList.add(docEntry);
		}
		queryResponse.setDocumentEntries(documentEntryList);		
		return queryResponse;	
	}
	
	public QueryResponse  GET_FOLDERS(GetFoldersQuery getFoldersQuery) {
		QueryResponse queryResponse = new QueryResponse();
		
		return queryResponse;	
	}
	
	public QueryResponse GET_ASSOCIATIONS(GetAssociationsQuery findDocumentsQuery) {
		QueryResponse queryResponse = new QueryResponse();	
		GetAssociationsVO vo = BuildVoUtil.buildGetAssociationsVO(findDocumentsQuery);
		XdsAssociationPO requestPO = new XdsAssociationPO();
		requestPO.setEntryUuid(vo.get$uuid());
		requestPO.setHomeCommunityId(vo.get$homeCommunityId());				
		List<XdsAssociationPO> xdsAssociationList = findBaseDao_CAMEL.GET_ASSOCIATIONS(requestPO);
		List<Association>  associations =new ArrayList<Association>();
		for(XdsAssociationPO po:xdsAssociationList) {
	        Association docFolderAssociation = new Association();
	        xdsAssociationPOToAssociation(po,docFolderAssociation);
	        associations.add(docFolderAssociation);
		}
		queryResponse.setAssociations(associations);
		log.info(JaxbMapperUtils.convertToXml(queryResponse, "UTF-8", true));		
		return queryResponse;		
	}
	
	
	public QueryResponse GET_DOCUMENTS_AND_ASSOCIATIONS(GetDocumentsAndAssociationsQuery findDocumentsQuery) {
		QueryResponse queryResponse = new QueryResponse();	
		GetDocumentsAndAssociationsVO vo = BuildVoUtil.buildGetDocumentsAndAssociationsVO(findDocumentsQuery);
		XdsDocumentEntryPO requestPO = new XdsDocumentEntryPO();
		requestPO.setEntryUuid(vo.get$XDSDocumentEntryEntryUUID());
		requestPO.setUniqueId(vo.get$XDSDocumentEntryUniqueId());
		requestPO.setHomeCommunityId(vo.get$homeCommunityId());
		
		//查文档
		List<XdsDocumentEntryPO> xdsDocumentEntryPOList= findBaseDao_CAMEL.GET_XdsDocumentEntryPO(requestPO);
		List<DocumentEntry> documentEntryList = new ArrayList<DocumentEntry>();
		for(XdsDocumentEntryPO po:xdsDocumentEntryPOList) {
			
	        // 组装DocumentEntry
	        DocumentEntry docEntry = new DocumentEntry();
			xdsDocumentEntryPOToDocumentEntry(po,docEntry);			
	        documentEntryList.add(docEntry);
	        
	        //查目标关系
			XdsAssociationPO requestPO1 = new XdsAssociationPO();
			requestPO1.setTargetUuid(po.getEntryUuid());;				
			List<XdsAssociationPO> xdsAssociationList = findBaseDao_CAMEL.GET_ASSOCIATIONS(requestPO1);
			List<Association>  associations =new ArrayList<Association>();
			for(XdsAssociationPO xdsAssociationPO:xdsAssociationList) {
		        Association docFolderAssociation = new Association();
		        xdsAssociationPOToAssociation(xdsAssociationPO,docFolderAssociation);	
		        associations.add(docFolderAssociation);
			}
	            
	        //查源关系
			XdsAssociationPO requestPO2 = new XdsAssociationPO();
			requestPO2.setSourceUuid(po.getEntryUuid());			
			List<XdsAssociationPO> xdsAssociationList2 = findBaseDao_CAMEL.GET_ASSOCIATIONS(requestPO2);
			for(XdsAssociationPO xdsAssociationPO:xdsAssociationList2) {
		        Association docFolderAssociation = new Association();
		        xdsAssociationPOToAssociation(xdsAssociationPO,docFolderAssociation);		
		        associations.add(docFolderAssociation);
			}
			queryResponse.setAssociations(associations);
		}
	    queryResponse.setDocumentEntries(documentEntryList);
		log.info(JaxbMapperUtils.convertToXml(queryResponse, "UTF-8", true));
		return queryResponse;
		
		
	}
	
	public QueryResponse GET_SUBMISSION_SETS(GetSubmissionSetsQuery findDocumentsQuery) {
		QueryResponse queryResponse = new QueryResponse();	
		GetSubmissionSetsVO vo = BuildVoUtil.buildGetSubmissionSetsVO(findDocumentsQuery);
		XdsSubmissionSetPO requestPO = new XdsSubmissionSetPO();
		requestPO.setEntryUuid(vo.get$uuid());
		requestPO.setHomeCommunityId(vo.get$homeCommunityId());
		List<SubmissionSet> submissionSets = new ArrayList<SubmissionSet>();
		
		List<XdsSubmissionSetPO> xdsList = findBaseDao_CAMEL.GET_XdsSubmissionSetPO(requestPO);
		for(XdsSubmissionSetPO ox:xdsList) {
			SubmissionSet submissionSet =new SubmissionSet();
			xdsSubmissionSetPOToSubmissionSet(ox,submissionSet);
			submissionSets.add(submissionSet);	    
			
			//查源关系
			List<Association>  associations =new ArrayList<Association>();
			XdsAssociationPO requestPO2 = new XdsAssociationPO();
			requestPO2.setSourceUuid(ox.getEntryUuid());			
			List<XdsAssociationPO> xdsAssociationList2 = findBaseDao_CAMEL.GET_ASSOCIATIONS(requestPO2);
			for(XdsAssociationPO xdsAssociationPO:xdsAssociationList2) {
		        Association docFolderAssociation = new Association();
		        xdsAssociationPOToAssociation(xdsAssociationPO,docFolderAssociation);		
		        associations.add(docFolderAssociation);
			}
			queryResponse.setAssociations(associations);
			
			
		}		
		queryResponse.setSubmissionSets(submissionSets);
		log.info(JaxbMapperUtils.convertToXml(queryResponse, "UTF-8", true));
		return queryResponse;	
		
	}
	
	public QueryResponse GET_FOLDER_AND_CONTENTS(GetFolderAndContentsQuery findDocumentsQuery) {
		QueryResponse queryResponse = new QueryResponse();	

		GetFolderAndContentsVO vo = BuildVoUtil.buildGetFolderAndContentsVO(findDocumentsQuery);
				
		XdsFolderPO requestPO = new XdsFolderPO();
		requestPO.setEntryUuid(vo.get$XDSFolderEntryUUID());
		requestPO.setUniqueId(vo.get$XDSFolderUniqueId());
		requestPO.setHomeCommunityId(vo.get$homeCommunityId());
		
		List<XdsFolderPO> xdsFolderPOList = findBaseDao_CAMEL.GET_XdsFolderPO(requestPO);
		List<Folder> folderList = new ArrayList<Folder>();
		
		for(XdsFolderPO ox:xdsFolderPOList) {			
	        Folder folder = new Folder();
	        xdsFolderPOToFolder(ox,folder);
	        folderList.add(folder);
		}
		queryResponse.setFolders(folderList);
		log.info(JaxbMapperUtils.convertToXml(queryResponse, "UTF-8", true));
		return queryResponse;
		
		
	}
	public QueryResponse GET_FOLDERS_FOR_DOCUMENT(GetFoldersForDocumentQuery findDocumentsQuery) {
		QueryResponse queryResponse = new QueryResponse();	
		GetFoldersForDocumentVO vo = BuildVoUtil.buildGetFoldersForDocumentVO(findDocumentsQuery);				
		XdsFolderPO requestPO = new XdsFolderPO();
		requestPO.setEntryUuid(vo.get$XDSDocumentEntryEntryUUID());
		requestPO.setUniqueId(vo.get$XDSDocumentEntryUniqueId());
		requestPO.setHomeCommunityId(vo.get$homeCommunityId());	
		List<XdsFolderPO> xdsFolderPOList = findBaseDao_CAMEL.GET_XdsFolderPO(requestPO);
		List<Folder> folderList = new ArrayList<Folder>();		
		for(XdsFolderPO ox:xdsFolderPOList) {			
	        Folder folder = new Folder();
	        xdsFolderPOToFolder(ox,folder);
	        folderList.add(folder);
		}
		queryResponse.setFolders(folderList);
		log.info(JaxbMapperUtils.convertToXml(queryResponse, "UTF-8", true));
		return queryResponse;
		
	}
	
	public QueryResponse GET_ALL(GetAllQuery findDocumentsQuery) {
		QueryResponse queryResponse = new QueryResponse();	
		GetAllVO vo = BuildVoUtil.buildGetAllVO(findDocumentsQuery);
		
		//查询提交集
		XdsSubmissionSetPO requestPO = new XdsSubmissionSetPO();
		requestPO.setPatientId(vo.get$patientId());
		requestPO.setAvailabilityStatus(vo.get$XDSSubmissionSetStatus());
		List<SubmissionSet> submissionSets = new ArrayList<SubmissionSet>();
		
		List<XdsSubmissionSetPO> xdsList = findBaseDao_CAMEL.GET_XdsSubmissionSetPO(requestPO);
		for(XdsSubmissionSetPO ox:xdsList) {
			SubmissionSet submissionSet =new SubmissionSet();
			xdsSubmissionSetPOToSubmissionSet(ox,submissionSet);
			submissionSets.add(submissionSet);	
		}		
		queryResponse.setSubmissionSets(submissionSets);
				
		//查询文件夹
		XdsFolderPO requestPO1 = new XdsFolderPO();
		requestPO1.setPatientId(vo.get$patientId());
		requestPO1.setAvailabilityStatus(vo.get$XDSFolderStatus());
		
		List<XdsFolderPO> xdsFolderPOList = findBaseDao_CAMEL.GET_XdsFolderPO(requestPO1);
		List<Folder> folderList = new ArrayList<Folder>();
		
		for(XdsFolderPO ox:xdsFolderPOList) {			
	        Folder folder = new Folder();
	        xdsFolderPOToFolder(ox,folder);
	        folderList.add(folder);
		}
		queryResponse.setFolders(folderList);
		//查询文档
		XdsDocumentEntryPO requestPO2 = new XdsDocumentEntryPO();
		requestPO2.setPatientId(vo.get$patientId());
		requestPO2.setDocumentAvailability(vo.get$XDSDocumentEntryStatus());
		requestPO2.setDocumentEntryType(vo.get$XDSDocumentEntryType());
		requestPO2.setConfidentialityCodes(vo.get$XDSDocumentEntryConfidentialityCode());
		requestPO2.setFormatCode(vo.get$XDSDocumentEntryFormatCode());
		
		//查文档及查询关联关系
		List<XdsDocumentEntryPO> xdsDocumentEntryPOList= findBaseDao_CAMEL.GET_XdsDocumentEntryPO(requestPO2);
		List<DocumentEntry> documentEntryList = new ArrayList<DocumentEntry>();
		for(XdsDocumentEntryPO po:xdsDocumentEntryPOList) {
	        // 组装DocumentEntry
	        DocumentEntry docEntry = new DocumentEntry();	        
	        xdsDocumentEntryPOToDocumentEntry(po,docEntry);	
	        documentEntryList.add(docEntry);
	        
	        //查目标关系
			XdsAssociationPO requestPO11 = new XdsAssociationPO();
			requestPO11.setTargetUuid(po.getEntryUuid());;				
			List<XdsAssociationPO> xdsAssociationList = findBaseDao_CAMEL.GET_ASSOCIATIONS(requestPO11);
			List<Association>  associations =new ArrayList<Association>();
			for(XdsAssociationPO xdsAssociationPO:xdsAssociationList) {
		        Association docFolderAssociation = new Association();
		        xdsAssociationPOToAssociation(xdsAssociationPO,docFolderAssociation);	
		        associations.add(docFolderAssociation);
			}
	            
	        //查源关系
			XdsAssociationPO requestPO21 = new XdsAssociationPO();
			requestPO21.setSourceUuid(po.getEntryUuid());			
			List<XdsAssociationPO> xdsAssociationList2 = findBaseDao_CAMEL.GET_ASSOCIATIONS(requestPO21);
			for(XdsAssociationPO xdsAssociationPO:xdsAssociationList2) {
		        Association docFolderAssociation = new Association();
		        xdsAssociationPOToAssociation(xdsAssociationPO,docFolderAssociation);	
		        associations.add(docFolderAssociation);
			}
			queryResponse.setAssociations(associations);
		}
	    queryResponse.setDocumentEntries(documentEntryList);
		
		log.info(JaxbMapperUtils.convertToXml(queryResponse, "UTF-8", true));

		return queryResponse;
			
		
	}
	public QueryResponse GET_SUBMISSION_SET_AND_CONTENTS(GetSubmissionSetAndContentsQuery getSubmissionSetAndContentsQuery) {
		
		QueryResponse queryResponse = new QueryResponse();
		GetSubmissionSetAndContentsVO vo = BuildVoUtil
				.buildGetSubmissionSetAndContentsVO(getSubmissionSetAndContentsQuery);

		// 查询提交集
		XdsSubmissionSetPO requestPO = new XdsSubmissionSetPO();
		requestPO.setUniqueId(vo.get$XDSSubmissionSetUniqueId());
		requestPO.setEntryUuid(vo.get$XDSSubmissionSetEntryUUID());
		List<SubmissionSet> submissionSets = new ArrayList<SubmissionSet>();

		List<XdsSubmissionSetPO> xdsList = findBaseDao_CAMEL.GET_XdsSubmissionSetPO(requestPO);
		for (XdsSubmissionSetPO ox : xdsList) {
			SubmissionSet submissionSet = new SubmissionSet();
			xdsSubmissionSetPOToSubmissionSet(ox,submissionSet);		
			submissionSets.add(submissionSet);

			// 查目标关系
			XdsAssociationPO requestPO11 = new XdsAssociationPO();
			requestPO11.setSourceUuid(ox.getEntryUuid());
			List<XdsAssociationPO> xdsAssociationList = findBaseDao_CAMEL.GET_ASSOCIATIONS(requestPO11);
			List<Association> associations = new ArrayList<Association>();
			List<DocumentEntry> documentEntryList = new ArrayList<DocumentEntry>();
			List<Folder> folderList = new ArrayList<Folder>();
			for (XdsAssociationPO xdsAssociationPO : xdsAssociationList) {
				Association docFolderAssociation = new Association();
		        xdsAssociationPOToAssociation(xdsAssociationPO,docFolderAssociation);
				associations.add(docFolderAssociation);

				// 查询文档
				XdsDocumentEntryPO requestPO2 = new XdsDocumentEntryPO();
				requestPO2.setEntryUuid(xdsAssociationPO.getTargetUuid());
				requestPO2.setHomeCommunityId(vo.get$homeCommunityId());
				requestPO2.setDocumentEntryType(vo.get$XDSDocumentEntryType());
				requestPO2.setConfidentialityCodes(vo.get$XDSDocumentEntryConfidentialityCode());
				requestPO2.setFormatCode(vo.get$XDSDocumentEntryFormatCode());

				// 查文档及查询关联关系
				List<XdsDocumentEntryPO> xdsDocumentEntryPOList = findBaseDao_CAMEL.GET_XdsDocumentEntryPO(requestPO2);
				for (XdsDocumentEntryPO po : xdsDocumentEntryPOList) {

			        // 组装DocumentEntry
			        DocumentEntry docEntry = new DocumentEntry();	        
			        xdsDocumentEntryPOToDocumentEntry(po,docEntry);	
					documentEntryList.add(docEntry);

				}
				
				// 查询文件夹
				XdsFolderPO requestPO1 = new XdsFolderPO();
				List<XdsFolderPO> xdsFolderPOList = findBaseDao_CAMEL.GET_XdsFolderPO(requestPO1);
				for (XdsFolderPO ox1 : xdsFolderPOList) {
					Folder folder = new Folder();
			        xdsFolderPOToFolder(ox1,folder);
					folderList.add(folder);
				}				
			}
			queryResponse.setDocumentEntries(documentEntryList);
			queryResponse.setAssociations(associations);
			queryResponse.setFolders(folderList);

		}
		queryResponse.setSubmissionSets(submissionSets);
		log.info(JaxbMapperUtils.convertToXml(queryResponse, "UTF-8", true));

		return queryResponse;	
		
	}
	public QueryResponse  GET_RELATED_DOCUMENTS(GetRelatedDocumentsQuery getRelatedDocumentsQuery) {
		QueryResponse queryResponse = new QueryResponse();
		
		return queryResponse;	
	}
	
	public QueryResponse  FIND_DOCUMENTS_BY_REFERENCE_ID(FindDocumentsByReferenceIdQuery findDocumentsByReferenceIdQuery) {
		QueryResponse queryResponse = new QueryResponse();
		
		return queryResponse;	
	}

	
	@SuppressWarnings("unchecked")
	public void  xdsSubmissionSetPOToSubmissionSet(XdsSubmissionSetPO ox,SubmissionSet submissionSet) {
		submissionSet.setEntryUuid(ox.getEntryUuid());
		submissionSet.setHomeCommunityId(ox.getHomeCommunityId());
		submissionSet.setLogicalUuid(ox.getLogicalUuid());
		
		submissionSet.setSubmissionTime(DateUtils.changeTime2(ox.getSubmissionTime()));
		submissionSet.setAvailabilityStatus(AvailabilityStatus.valueOfOpcode(ox.getAvailabilityStatus()));
		submissionSet.setSourceId(ox.getSourceId());
		submissionSet.setUniqueId(ox.getUniqueId());
		submissionSet.setLimitedMetadata(false);		
		
	    if(DataCacheMap.get("SubmissionSet-patientID"+ox.getEntryUuid())!=null) {
	    	submissionSet.setPatientId((Identifiable)DataCacheMap.get("SubmissionSet-patientID"+ox.getEntryUuid()));
	    } 

		 // 设置作者信息
        if(DataCacheMap.get("SubmissionSet-author"+ox.getEntryUuid())!=null) {
        	List<Author> authorList=(List<Author>)DataCacheMap.get("SubmissionSet-author"+ox.getEntryUuid());
        	for(Author author : authorList) {
            	submissionSet.setAuthor(author);
        	}
        }
        if(DataCacheMap.get("SubmissionSet-title"+ox.getEntryUuid())!=null) {
        	submissionSet.setTitle((LocalizedString)DataCacheMap.get("SubmissionSet-title"+ox.getEntryUuid()));
        }
        if(DataCacheMap.get("SubmissionSet-code"+ox.getEntryUuid())!=null) {
        	submissionSet.setContentTypeCode((Code)DataCacheMap.get("SubmissionSet-code"+ox.getEntryUuid()));
        }
        if(DataCacheMap.get("SubmissionSet-extraMetadata"+ox.getEntryUuid())!=null) {
        	submissionSet.setExtraMetadata((Map<String,List<String>>)DataCacheMap.get("SubmissionSet-extraMetadata"+ox.getEntryUuid()));	
        }
        if(DataCacheMap.get("SubmissionSet-ebXMLClassificationList"+ox.getEntryUuid())!=null) {
        	submissionSet.getExtraClassifications().addAll((List<EbXMLClassification>)DataCacheMap.get("SubmissionSet-ebXMLClassificationList"+ox.getEntryUuid()));
        }
        if(DataCacheMap.get("SubmissionSet-version"+ox.getEntryUuid())!=null) {
        	submissionSet.setVersion((Version)DataCacheMap.get("SubmissionSet-version"+ox.getEntryUuid()));	
        }else {
        	submissionSet.setVersion(new Version());
        }
	}

	@SuppressWarnings("unchecked")
	public void  xdsDocumentEntryPOToDocumentEntry(XdsDocumentEntryPO po,DocumentEntry docEntry) {
		
		// 设置作者信息
        if(DataCacheMap.get("DocumentEntry-author"+po.getEntryUuid())!=null) {
          	List<Author> authorList=(List<Author>)DataCacheMap.get("DocumentEntry-author"+po.getEntryUuid());
        	for(Author author : authorList) {
                docEntry.getAuthors().add(author);
        	}
        }

        docEntry.setAvailabilityStatus(AvailabilityStatus.APPROVED);
        if(DataCacheMap.get("DocumentEntry-classCode"+po.getEntryUuid())!=null) {
            docEntry.setClassCode((Code)DataCacheMap.get("DocumentEntry-classCode"+po.getEntryUuid()));
        }
        if(DataCacheMap.get("DocumentEntry-comment"+po.getEntryUuid())!=null) {
            docEntry.setComments((LocalizedString)DataCacheMap.get("DocumentEntry-comment"+po.getEntryUuid()));
        }
        if(DataCacheMap.get("DocumentEntry-confidentialityCodes"+po.getEntryUuid())!=null) {
            docEntry.getConfidentialityCodes().addAll((List<Code>)DataCacheMap.get("DocumentEntry-confidentialityCodes"+po.getEntryUuid()));
        }
        
        docEntry.setCreationTime(DateUtils.changeTime2(po.getCreationTime()));

        if(DataCacheMap.get("DocumentEntry-eventCodes"+po.getEntryUuid())!=null) {
            docEntry.getEventCodeList().addAll((List<Code>)DataCacheMap.get("DocumentEntry-eventCodes"+po.getEntryUuid()));
        }
        
        if(DataCacheMap.get("DocumentEntry-formatCode"+po.getEntryUuid())!=null) {
            docEntry.setFormatCode((Code)DataCacheMap.get("DocumentEntry-formatCode"+po.getEntryUuid()));
        }

        docEntry.setEntryUuid(po.getEntryUuid());
        docEntry.setLogicalUuid(po.getLogicalUuid());
        docEntry.setHash(po.getHash());
        
        if(DataCacheMap.get("DocumentEntry-healthcareFacilityTypeCode"+po.getEntryUuid())!=null) {
            docEntry.setHealthcareFacilityTypeCode((Code)DataCacheMap.get("DocumentEntry-healthcareFacilityTypeCode"+po.getEntryUuid()));
        }       
        docEntry.setLanguageCode(po.getLanguageCode());
        
        if(DataCacheMap.get("DocumentEntry-legalAuthenticator"+po.getEntryUuid())!=null) {
            docEntry.setLegalAuthenticator((Person)DataCacheMap.get("DocumentEntry-legalAuthenticator"+po.getEntryUuid()));
        }    
        docEntry.setMimeType(po.getMimeType());

        if(DataCacheMap.get("DocumentEntry-patientID"+po.getEntryUuid())!=null) {
            docEntry.setPatientId((Identifiable)DataCacheMap.get("DocumentEntry-patientID"+po.getEntryUuid()));
        }    
       
        if(DataCacheMap.get("DocumentEntry-practiceSettingCode"+po.getEntryUuid())!=null) {
            docEntry.setPracticeSettingCode((Code)DataCacheMap.get("DocumentEntry-practiceSettingCode"+po.getEntryUuid()));
        }   
        docEntry.setRepositoryUniqueId(po.getRepositoryUniqueId());
        docEntry.setServiceStartTime(DateUtils.changeTime2(po.getServiceStartTime()));
        docEntry.setServiceStopTime(DateUtils.changeTime2(po.getServiceStopTime()));
        docEntry.setSize(po.getSize());
        // 设置源患者信息
        if(DataCacheMap.get("DocumentEntry-sourcepatientID"+po.getEntryUuid())!=null) {
            docEntry.setSourcePatientId((Identifiable)DataCacheMap.get("DocumentEntry-sourcepatientID"+po.getEntryUuid()));
        }    
        
        // 设置患者基本信息
        PatientInfo patientInfo = new PatientInfo();
        if(DataCacheMap.get("DocumentEntry-patientInfo"+po.getEntryUuid())!=null) {
        	patientInfo=(PatientInfo)DataCacheMap.get("DocumentEntry-patientInfo"+po.getEntryUuid());
        }
        docEntry.setSourcePatientInfo(patientInfo);
        
        if(DataCacheMap.get("DocumentEntry-title"+po.getEntryUuid())!=null) {
            docEntry.setTitle((LocalizedString)DataCacheMap.get("DocumentEntry-title"+po.getEntryUuid()));
        }
        if(DataCacheMap.get("DocumentEntry-typeCode"+po.getEntryUuid())!=null) {
            docEntry.setTypeCode((Code)DataCacheMap.get("DocumentEntry-typeCode"+po.getEntryUuid()));
        }   

        docEntry.setUniqueId(po.getUniqueId());
        docEntry.setUri(po.getUri());
        if(DataCacheMap.get("DocumentEntry-referenceIdList"+po.getEntryUuid())!=null) {
        	docEntry.getReferenceIdList().addAll((List<ReferenceId>)DataCacheMap.get("DocumentEntry-referenceIdList"+po.getEntryUuid()));	
        }
        if(DataCacheMap.get("DocumentEntry-extraMetadata"+po.getEntryUuid())!=null) {
        	docEntry.setExtraMetadata((Map<String,List<String>>)DataCacheMap.get("DocumentEntry-extraMetadata"+po.getEntryUuid()));	
        }
        if(DataCacheMap.get("DocumentEntry-ebXMLClassificationList"+po.getEntryUuid())!=null) {
        	docEntry.setExtraClassifications((List<EbXMLClassification>)DataCacheMap.get("DocumentEntry-ebXMLClassificationList"+po.getEntryUuid()));	
        }
        if(DataCacheMap.get("DocumentEntry-version"+po.getEntryUuid())!=null) {
        	docEntry.setVersion((Version)DataCacheMap.get("DocumentEntry-version"+po.getEntryUuid()));	
        }else {
            docEntry.setVersion(new Version());
        }
        if(DataCacheMap.get("DocumentEntry-legalAuthenticator"+po.getEntryUuid())!=null) {
        	docEntry.setLegalAuthenticator((Person)DataCacheMap.get("DocumentEntry-legalAuthenticator"+po.getEntryUuid()));	
        }

	}
	
	@SuppressWarnings("unchecked")
	public void  xdsFolderPOToFolder(XdsFolderPO ox,Folder folder) {
        folder.setAvailabilityStatus(AvailabilityStatus.valueOfOpcode(ox.getAvailabilityStatus()));
        folder.setEntryUuid(ox.getEntryUuid());
//        folder.setLastUpdateTime(ox.getUpdateTime());
        Identifiable patientID = new Identifiable(ox.getPatientId(), new AssigningAuthority());
        folder.setPatientId(patientID);
        folder.setUniqueId(ox.getUniqueId());
        if(DataCacheMap.get("Folder-title"+ox.getEntryUuid())!=null) {
	        folder.setTitle((LocalizedString)DataCacheMap.get("Folder-title"+ox.getEntryUuid()));
        }
        if(DataCacheMap.get("Folder-comment"+ox.getEntryUuid())!=null) {
	        folder.setComments((LocalizedString)DataCacheMap.get("Folder-comment"+ox.getEntryUuid()));
        }
        if(DataCacheMap.get("Folder-codeList"+ox.getEntryUuid())!=null) {
        	folder.getCodeList().addAll((List<Code>)DataCacheMap.get("Folder-codeList"+ox.getEntryUuid()));
        }
        if(DataCacheMap.get("Folder-extraMetadata"+ox.getEntryUuid())!=null) {
        	folder.setExtraMetadata((Map<String,List<String>>)DataCacheMap.get("Folder-extraMetadata"+ox.getEntryUuid()));	
        }
        if(DataCacheMap.get("Folder-ebXMLClassificationList"+ox.getEntryUuid())!=null) {
        	folder.setExtraClassifications((List<EbXMLClassification>)DataCacheMap.get("Folder-ebXMLClassificationList"+ox.getEntryUuid()));	
        }	        folder.setAvailabilityStatus(AvailabilityStatus.valueOfOpcode(ox.getAvailabilityStatus()));
        folder.setEntryUuid(ox.getEntryUuid());

	}
	
	@SuppressWarnings("unchecked")
	public void  xdsAssociationPOToAssociation(XdsAssociationPO po,Association association) {
		association.setAssociationType(AssociationType.valueOfOpcode30(po.getAssociationType()));
		association.setSourceUuid(po.getSourceUuid());
		association.setTargetUuid(po.getTargetUuid());
		association.setEntryUuid(po.getEntryUuid());
		if(po.getOriginalStatus()!=null) {
			association.setOriginalStatus(AvailabilityStatus.valueOf(po.getOriginalStatus()));	
		}
		if(po.getAvailabilityStatus()!=null) {
			association.setAvailabilityStatus(AvailabilityStatus.valueOf(po.getAvailabilityStatus()));	
		}
		if(po.getNewStatus()!=null) {
			association.setNewStatus(AvailabilityStatus.valueOf(po.getNewStatus()));	
		}
		association.setLabel(AssociationLabel.fromOpcode(po.getAssociationLabel()));
        if(DataCacheMap.get("Association-extraMetadata"+po.getEntryUuid())!=null) {
        	association.setExtraMetadata((Map<String,List<String>>)DataCacheMap.get("Association-extraMetadata"+po.getEntryUuid()));	
        }
        if(DataCacheMap.get("Association-docCode"+po.getEntryUuid())!=null) {
        	association.setDocCode((Code)DataCacheMap.get("Association-docCode"+po.getEntryUuid()));	
        }
	}
}
